package com.wcgksoft.common.base;

import com.wcgksoft.dao.EntityDao;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * 类名称：BaseService
 * 类描述：
 * 创建人：
 * 创建时间：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @version 1.0.0
 */
@Service
public class BaseService<T> {
    @Autowired
    private EntityDao<T> entityDao;
    private long totalCount = 0;

    /**
     * @param criteria
     * @param startIndex
     * @param pageSize
     * @return
     */
    public List<T> findByConditions(DetachedCriteria criteria, int startIndex, int pageSize) {
        List<T> list = entityDao.findByConditions(criteria, startIndex, pageSize);
        this.totalCount = entityDao.getTotalCount();
        System.out.println(this.totalCount);
      /*  Session session = entityDao.getCurrentSession();
        Query query = session.createQuery("from  Area ");
        System.out.println(query.list());*/
        return list;
    }

    //
    public Session getHibernateSession() {
        return entityDao.getCurrentSession();
    }

    /**
     * @param criteria
     * @param startIndex
     * @param pageSize
     * @return
     */
    public List<T> findByConditions(DetachedCriteria criteria) {
        List<T> list = entityDao.findByConditions(criteria);
        return list;
    }


    /**
     * 根据id查询对象数据
     *
     * @param id
     * @return
     */
    public T findById(Serializable id) {
        return entityDao.findById(initClass(), id);
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void delete(T entity) {
        entityDao.delete(entity);
    }

    public void deleteCass(T entity) {
        entityDao.delete(entity);
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public  void save(T entity){
        entityDao.save(entity);
    }
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void saveOrUpdate(T entity) {
        entityDao.saveOrUpdate(entity);
    }
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public <T> T saveMerge(T entity) {
        return entityDao.saveMerge(entity);
    }

    public void saveUpdate(T entity) {
        entityDao.saveOrUpdate(entity);
    }
    /*
     * 修改
     * */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void upadte(T enity) {
        entityDao.update(enity);
    }

    /**
     * HQL批量删除
     *
     * @param entityName entity的名称
     * @param id         例如1,2,3,4,5,6
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void deleteAll(String entityName, String id) {
        String hql = "delete from " + entityName + " where id in(" + id + ")";
        entityDao.createHQLQuery(hql);
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void deleteAll(String entityName, Integer id) {
        String hql = "delete from " + entityName + " where id in(" + id + ")";
        entityDao.createHQLQuery(hql);
    }

    public List<T> findAll(Class beanClass) {
        return entityDao.findAll(beanClass);
    }

    @SuppressWarnings("unchecked")
    private Class<T> initClass() {
        Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return entityClass;
    }

	public long getTotalCount() {
		return totalCount;
	}
    
}
